package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 *
 * @author panda
 * @since 2023/1/5 15:08
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    //@PostMapping("/add")
    //public R<ShoppingCart> saveShop(@RequestBody ShoppingCart shoppingCart){
    //    log.info("购物车信息为:{}",shoppingCart.toString());
    //    //1,获取用户userId,在threadLocal当中,
    //    // 在过滤器中判断的时候已经将其放入了ThreadLocal当中,取出即可
    //    shoppingCart.setUserId(BaseContext.getCurrentId());
    //    //2,判断菜品Id是否在当前购物车当中
    //    //根据用户ID和购物车中菜品ID和套餐ID查询购物车内容
    //    //查询当前菜品或者套餐是否在购物车中
    //    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    //    queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
    //    if(shoppingCart.getDishId() != null){
    //        //添加到购物车的是菜品
    //        queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
    //    }else{
    //        //添加到购物车的是套餐
    //        queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
    //    }
    //    //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
    //    ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
    //    // 判断当前购物车是否存在
    //    if(cartServiceOne != null){
    //        //如果已经存在，就在原来数量基础上加一
    //        cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
    //        shoppingCartService.updateById(cartServiceOne);
    //    }else{
    //        //如果不存在，则添加到购物车，数量默认就是一
    //        shoppingCart.setNumber(1);
    //        shoppingCart.setCreateTime(LocalDateTime.now());
    //        shoppingCartService.save(shoppingCart);
    //        cartServiceOne = shoppingCart;
    //    }
    //    return R.success(cartServiceOne);
    //
    //    //3,如果存在则加一
    //
    //    //4,不存在,添加到购物车
    //
    //}
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //在Threadlocal中获取userId,放入shoppingCart购物车当中
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //将内容添加到购物车当中,并且进行判断
        ///添加的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据userId查询该用户购物车中的内容
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (shoppingCart.getDishId() != null) {
            //不为空,添加购物车中的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //为空,添加购物车中的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //在进行对比之后查询结果
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart != null) {
            //不为空,说明购物车中存在该菜品或者套餐,需要在基础上加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        } else {
            //为空,则添加的菜品或者套餐不存在购物车当中
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }
        return R.success(cart);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}